package com.ruoyi.campadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.campadmin.mapper.CaAmenityMapper;
import com.ruoyi.campadmin.service.ICaAmenityService;
import com.ruoyi.campadmin.domain.CaAmenity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.campadmin.domain.CaCamp;
import com.ruoyi.campadmin.service.ICaCampService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CaAmenityServiceImpl implements ICaAmenityService {


    @Resource
    private CaAmenityMapper caAmenityMapper;
    
    
    @Resource
    private ICaCampService caCampService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param caAmenity 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CaAmenity> getCaAmenityPage(PageDomain pageDomain, CaAmenity caAmenity , CaCamp caCamp) {
        if (Objects.isNull(caAmenity)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CaAmenity> queryWrapper = new LambdaQueryWrapper<>(new CaAmenity());
            //if(!Objects.isNull(caAmenity.getName())){
            //	queryWrapper.like(CaAmenity::getName,caAmenity.getName());
            //}
            

            if (!Objects.isNull(caAmenity.getId())) {
                queryWrapper.eq(CaAmenity::getId, caAmenity.getId());
            }

            if (!Objects.isNull(caAmenity.getLatitude())) {
                queryWrapper.like(CaAmenity::getLatitude, caAmenity.getLatitude());
            }

            if (!Objects.isNull(caAmenity.getLongitude())) {
                queryWrapper.like(CaAmenity::getLongitude, caAmenity.getLongitude());
            }

            if (!Objects.isNull(caAmenity.getCaCampId())) {
                queryWrapper.eq(CaAmenity::getCaCampId, caAmenity.getCaCampId());
            }

            caCamp.setBaseEntityNull();
            caCamp.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(caCamp)) {
                List<Long> idList = caCampService.getIdListByCaCamp(caCamp);
                queryWrapper.in(CaAmenity::getCaCampId, idList);
            }


            if (!Objects.isNull(caAmenity.getAmenityName())) {
                queryWrapper.like(CaAmenity::getAmenityName, caAmenity.getAmenityName());
            }

            if (!Objects.isNull(caAmenity.getPhotoUrl())) {
                queryWrapper.like(CaAmenity::getPhotoUrl, caAmenity.getPhotoUrl());
            }

            if (!Objects.isNull(caAmenity.getDetailText())) {
                queryWrapper.like(CaAmenity::getDetailText, caAmenity.getDetailText());
            }

            if (!Objects.isNull(caAmenity.getFeeType())) {
                queryWrapper.like(CaAmenity::getFeeType, caAmenity.getFeeType());
            }

            if (!Objects.isNull(caAmenity.getOnetimeFee())) {
                queryWrapper.like(CaAmenity::getOnetimeFee, caAmenity.getOnetimeFee());
            }

            if (!Objects.isNull(caAmenity.getPraiseNum())) {
                queryWrapper.like(CaAmenity::getPraiseNum, caAmenity.getPraiseNum());
            }

            if (!Objects.isNull(caAmenity.getCollectionNum())) {
                queryWrapper.like(CaAmenity::getCollectionNum, caAmenity.getCollectionNum());
            }

            if (!Objects.isNull(caAmenity.getStatus())) {
                queryWrapper.like(CaAmenity::getStatus, caAmenity.getStatus());
            }

            if (!Objects.isNull(caAmenity.getDelFlag())) {
                queryWrapper.like(CaAmenity::getDelFlag, caAmenity.getDelFlag());
            }

            if (!Objects.isNull(caAmenity.getCreateBy())) {
                queryWrapper.like(CaAmenity::getCreateBy, caAmenity.getCreateBy());
            }

            if (!Objects.isNull(caAmenity.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caAmenity.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caAmenity.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaAmenity::getCreateTime, caAmenity.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caAmenity.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaAmenity::getCreateTime, caAmenity.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caAmenity.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caAmenity.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaAmenity::getCreateTime, caAmenity.getParams().get("CreateTimeBeginTime"), caAmenity.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caAmenity.getUpdateBy())) {
                queryWrapper.like(CaAmenity::getUpdateBy, caAmenity.getUpdateBy());
            }

            if (!Objects.isNull(caAmenity.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caAmenity.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caAmenity.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaAmenity::getUpdateTime, caAmenity.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caAmenity.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaAmenity::getUpdateTime, caAmenity.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caAmenity.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caAmenity.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaAmenity::getUpdateTime, caAmenity.getParams().get("UpdateTimeBeginTime"), caAmenity.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caAmenity.getRemark())) {
                queryWrapper.like(CaAmenity::getRemark, caAmenity.getRemark());
            }
        Page<CaAmenity> caAmenityList= caAmenityMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CaAmenity record:caAmenityList.getRecords()){
            record.setCaCamp(caCampService.getCaCampById(record.getCaCampId()));
        }

            return caAmenityList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CaAmenity getCaAmenityById(Long id) {
        if (Objects.isNull(id)) {
            return new CaAmenity();
        }
        try {
            CaAmenity caAmenity = caAmenityMapper.selectById(id);
            caAmenity.setCaCamp(caCampService.getCaCampById(caAmenity.getCaCampId()));
            if (Objects.isNull(caAmenity)){
                caAmenity = new CaAmenity();
            }
            return caAmenity;

        } catch (Exception e) {
            e.printStackTrace();
            return new CaAmenity();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param caAmenity 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCaAmenity(CaAmenity caAmenity) {
        if (Objects.isNull(caAmenity)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CaAmenity> queryWrapper = new LambdaQueryWrapper<>(new CaAmenity());
            

            if (!Objects.isNull(caAmenity.getId())) {
                queryWrapper.eq(CaAmenity::getId, caAmenity.getId());
            }

            if (!Objects.isNull(caAmenity.getLatitude())) {
                queryWrapper.like(CaAmenity::getLatitude, caAmenity.getLatitude());
            }

            if (!Objects.isNull(caAmenity.getLongitude())) {
                queryWrapper.like(CaAmenity::getLongitude, caAmenity.getLongitude());
            }

            if (!Objects.isNull(caAmenity.getCaCampId())) {
                queryWrapper.eq(CaAmenity::getCaCampId, caAmenity.getCaCampId());
            }

            if (!Objects.isNull(caAmenity.getAmenityName())) {
                queryWrapper.like(CaAmenity::getAmenityName, caAmenity.getAmenityName());
            }

            if (!Objects.isNull(caAmenity.getPhotoUrl())) {
                queryWrapper.like(CaAmenity::getPhotoUrl, caAmenity.getPhotoUrl());
            }

            if (!Objects.isNull(caAmenity.getDetailText())) {
                queryWrapper.like(CaAmenity::getDetailText, caAmenity.getDetailText());
            }

            if (!Objects.isNull(caAmenity.getFeeType())) {
                queryWrapper.like(CaAmenity::getFeeType, caAmenity.getFeeType());
            }

            if (!Objects.isNull(caAmenity.getOnetimeFee())) {
                queryWrapper.like(CaAmenity::getOnetimeFee, caAmenity.getOnetimeFee());
            }

            if (!Objects.isNull(caAmenity.getPraiseNum())) {
                queryWrapper.like(CaAmenity::getPraiseNum, caAmenity.getPraiseNum());
            }

            if (!Objects.isNull(caAmenity.getCollectionNum())) {
                queryWrapper.like(CaAmenity::getCollectionNum, caAmenity.getCollectionNum());
            }

            if (!Objects.isNull(caAmenity.getStatus())) {
                queryWrapper.like(CaAmenity::getStatus, caAmenity.getStatus());
            }

            if (!Objects.isNull(caAmenity.getDelFlag())) {
                queryWrapper.like(CaAmenity::getDelFlag, caAmenity.getDelFlag());
            }

            if (!Objects.isNull(caAmenity.getCreateBy())) {
                queryWrapper.like(CaAmenity::getCreateBy, caAmenity.getCreateBy());
            }

            if (!Objects.isNull(caAmenity.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caAmenity.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caAmenity.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaAmenity::getCreateTime, caAmenity.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caAmenity.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaAmenity::getCreateTime, caAmenity.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caAmenity.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caAmenity.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaAmenity::getCreateTime, caAmenity.getParams().get("CreateTimeBeginTime"), caAmenity.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caAmenity.getUpdateBy())) {
                queryWrapper.like(CaAmenity::getUpdateBy, caAmenity.getUpdateBy());
            }

            if (!Objects.isNull(caAmenity.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caAmenity.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caAmenity.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaAmenity::getUpdateTime, caAmenity.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caAmenity.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaAmenity::getUpdateTime, caAmenity.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caAmenity.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caAmenity.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaAmenity::getUpdateTime, caAmenity.getParams().get("UpdateTimeBeginTime"), caAmenity.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caAmenity.getRemark())) {
                queryWrapper.like(CaAmenity::getRemark, caAmenity.getRemark());
            }

            List<CaAmenity> caAmenitys = caAmenityMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaAmenity caAmenityNo:caAmenitys){
                idList.add(caAmenityNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过caCamp外键idList查询idList
     *
     * @param caCampIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCaCampIdList(List<Long> caCampIdList) {
        if (Objects.isNull(caCampIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CaAmenity> queryWrapper = new LambdaQueryWrapper<>(new CaAmenity());
            queryWrapper.in(CaAmenity::getCaCampId, caCampIdList);
            List<CaAmenity> caAmenitys = caAmenityMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaAmenity caAmenity:caAmenitys){
                idList.add(caAmenity.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param caAmenity 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCaAmenity(CaAmenity caAmenity) {
        if (Objects.isNull(caAmenity)) {
            return -1;
        }
        try {
        
            //caAmenity.setSpell(PinYin.getPinYin(caAmenity.getName()));
            return caAmenityMapper.insert(caAmenity);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param caAmenity 实体类
     * @return 结果
     */
    @Override
    public int updateCaAmenityById(CaAmenity caAmenity) {
        if (Objects.isNull(caAmenity)) {
            return -1;
        }
        try {
            //caAmenity.setSpell(PinYin.getPinYin(caAmenity.getName()));
            return caAmenityMapper.updateById(caAmenity);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCaAmenityByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return caAmenityMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CaAmenity> getCaAmenityList() {
        try {
            LambdaQueryWrapper<CaAmenity> queryWrapper = new LambdaQueryWrapper<>(new CaAmenity());
            queryWrapper.isNotNull(CaAmenity::getId);
            return caAmenityMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
